package com.ftdigital.primemovers_operators.adpter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.ftdigital.primemovers_operators.R;
import com.ftdigital.primemovers_operators.model.AddOilInfo;


/**
 * Created by Curry on 2015-12-16.
 * 用油协议
 */
public class OilAgreementListViewAdapter extends RecyclerView.Adapter<OilAgreementListViewAdapter.ViewHolder> implements View.OnClickListener {

    private Context mContext;
    private AddOilInfo addOilInfo;
    public OilAgreementListViewAdapter(Context context,  AddOilInfo addOilInfo) {
        mContext = context;
        this.addOilInfo = addOilInfo;
    }
    private OnRecyclerViewItemClickListener mOnItemClickListener = null;



    @Override
    public void onClick(View view) {
        if (mOnItemClickListener != null) {
            //注意这里使用getTag方法获取数据
            mOnItemClickListener.onItemClick(view,(int)view.getTag());
        }
    }

    public void setOnItemClickListener(OnRecyclerViewItemClickListener listener) {
        this.mOnItemClickListener = listener;
    }


    //define interface
    public static interface OnRecyclerViewItemClickListener {
        void onItemClick(View view, int position);
    }

    @Override
    public OilAgreementListViewAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.oil_agreement_list_title_item, parent, false);
        ViewHolder viewHolder = new ViewHolder(view);
        //将创建的View注册点击事件
        view.setOnClickListener(this);
        return viewHolder;
    }

    @Override
    public void onBindViewHolder(OilAgreementListViewAdapter.ViewHolder holder, int position) {
        holder.itemView.setTag(position);

        holder.getOilType().setText(addOilInfo.getData().getList().get(position).getGas_type());
        holder.getPrice().setText(addOilInfo.getData().getList().get(position).getPrice()+" 元/升");
        holder.getMonthUse().setText(addOilInfo.getData().getList().get(position).getAmount());
        holder.getPriceHint().setText(addOilInfo.getData().getList().get(position).getDiscount1());
    }

    @Override
    public int getItemCount() {
        return addOilInfo.getData().getList().size();
    }


    class ViewHolder extends RecyclerView.ViewHolder{
        private TextView oilType;
        private TextView price;
        private TextView monthUse;
        private TextView priceHint;
        public ViewHolder(View itemView) {
            super(itemView);
            oilType = (TextView) itemView.findViewById(R.id.oil_type_content);
            price = (TextView) itemView.findViewById(R.id.oil_price_content);
            monthUse = (TextView) itemView.findViewById(R.id.oil_month_use_content);
            priceHint = (TextView) itemView.findViewById(R.id.oil_price_hint_content);
        }

        public TextView getOilType() {
            return oilType;
        }

        public TextView getPrice() {
            return price;
        }

        public TextView getMonthUse() {
            return monthUse;
        }

        public TextView getPriceHint() {
            return priceHint;
        }
    }
}
